package com.yxinsur.imc.web.controller.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxinsur.imc.business.domain.ImcIns;
import com.yxinsur.imc.business.domain.ImcRepoIns;
import com.yxinsur.imc.business.domain.ImcRepoSupplier;
import com.yxinsur.imc.business.domain.ImcSupplier;
import com.yxinsur.imc.business.enums.IsDeletedEnum;
import com.yxinsur.imc.business.form.RepoSupplierForm;
import com.yxinsur.imc.business.service.IImcRepoSupplierService;
import com.yxinsur.imc.business.service.IImcSupplierService;
import com.yxinsur.imc.business.vo.RepoSupplierVo;
import com.yxinsur.imc.business.vo.select.SelectIns;
import com.yxinsur.imc.business.vo.select.SelectSupplier;
import com.yxinsur.imc.common.annotation.Log;
import com.yxinsur.imc.common.core.controller.BaseController;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.core.page.TableDataInfo;
import com.yxinsur.imc.common.enums.BusinessType;
import com.yxinsur.imc.common.utils.SecurityUtils;
import com.yxinsur.imc.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 供应商库Controller
 * 
 * @author imc
 * @date 2021-03-25
 */
@Api(value="供应商/保险公司仓库接口服务",tags={"5.1.供应商仓库接口服务"})
@RestController
@RequestMapping("/business/repo/supplier")
public class ImcRepoSupplierController extends BaseController
{
    @Autowired
    private IImcRepoSupplierService imcRepoSupplierService;
    @Autowired
    private IImcSupplierService imcSupplierService;

    /**
     * 查询供应商库列表
     */
    //@PreAuthorize("@ss.hasPermi('business:supplier:list')")
    @ApiOperation("供应商/保险公司仓库-列表分页数据查询")
    @GetMapping("/list")
    public TableDataInfo list(RepoSupplierForm supplierForm)
    {
        startPage();
        List<RepoSupplierVo> list = imcRepoSupplierService.selectImcRepoSupplierList(supplierForm);
        return getDataTable(list);
    }

    /**
     * 导出供应商库列表
     */
    //@PreAuthorize("@ss.hasPermi('business:supplier:export')")
    @ApiOperation("供应商/保险公司仓库-导出数据查询")
    @Log(title = "供应商库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RepoSupplierForm supplierForm)
    {
        List<RepoSupplierVo> list = imcRepoSupplierService.selectImcRepoSupplierList(supplierForm);
        ExcelUtil<RepoSupplierVo> util = new ExcelUtil<RepoSupplierVo>(RepoSupplierVo.class);
        return util.exportExcel(list, "供应商列表数据");
    }

    /**
     * 新增供应商库
     */
    //@PreAuthorize("@ss.hasPermi('business:supplier:add')")
    @ApiOperation("供应商/保险公司仓库-新增")
    @Log(title = "供应商库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RepoSupplierForm supplierForm)
    {
        return toAjax(imcRepoSupplierService.insertImcRepoSupplier(supplierForm));
    }

    /**
     * 修改供应商库
     */
    //@PreAuthorize("@ss.hasPermi('business:supplier:edit')")
    @ApiOperation("供应商/保险公司仓库-更新")
    @Log(title = "供应商库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RepoSupplierForm supplierForm)
    {
        return toAjax(imcRepoSupplierService.updateImcRepoSupplier(supplierForm));
    }

    /**
     * 删除供应商库
     */
    //@PreAuthorize("@ss.hasPermi('business:supplier:remove')")
    @Log(title = "供应商库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(0);
    }

    /**
     * 获取非我的供应商数据
     */
    @ApiOperation("获取非我的供应商数据")
    @PostMapping("/get/notmysuppliers")
    public List<SelectSupplier> notmysuppliers()
    {
        QueryWrapper<ImcSupplier> supWrapper = new QueryWrapper<>();
        supWrapper.eq("is_deleted", IsDeletedEnum.N.getCode());
        supWrapper.eq("tenant_id", SecurityUtils.getTenantId());
        List<ImcSupplier> supplierList = this.imcSupplierService.list(supWrapper);
        List<Long> repoIds = new ArrayList<>();
        repoIds.add(-1L);
        supplierList.stream().forEach(s ->{
            repoIds.add(s.getRepoSupplierId());
        });
        //查询权限配置的供应商列表
        QueryWrapper<ImcRepoSupplier> repoSupplierWrapper = new QueryWrapper<>();
        repoSupplierWrapper.notIn("id",repoIds);
        repoSupplierWrapper.eq("is_deleted",IsDeletedEnum.N.getCode());
        //repoSupplierWrapper.eq("tenant_id",SecurityUtils.getTenantId());
        List<ImcRepoSupplier> repoInsList = this.imcRepoSupplierService.list(repoSupplierWrapper);
        List<SelectSupplier> selectList = new ArrayList<>();
        repoInsList.stream().forEach(i ->{
            SelectSupplier selectSupplier = new SelectSupplier();
            BeanUtils.copyProperties(i,selectSupplier);
            selectList.add(selectSupplier);
        });
        return selectList;
    }
    /**
     * 获取非我的供应商数据
     */
    @ApiOperation("获取所有供应商数据")
    @PostMapping("/all")
    public List<SelectSupplier> allDataList()
    {
        //查询权限配置的供应商列表
        QueryWrapper<ImcRepoSupplier> repoSupplierWrapper = new QueryWrapper<>();
        repoSupplierWrapper.eq("is_deleted",IsDeletedEnum.N.getCode());
        List<ImcRepoSupplier> repoInsList = this.imcRepoSupplierService.list(repoSupplierWrapper);
        List<SelectSupplier> selectList = new ArrayList<>();
        repoInsList.stream().forEach(i ->{
            SelectSupplier selectSupplier = new SelectSupplier();
            BeanUtils.copyProperties(i,selectSupplier);
            selectList.add(selectSupplier);
        });
        return selectList;
    }
}
